package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.MechanicInfo;
import co.yixiang.modules.shop.service.MechanicInfoService;
import co.yixiang.modules.shop.service.dto.MechanicInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-08
*/
@Api(tags = "机修师管理")
@RestController
@RequestMapping("/api/mechanicInfo")
public class MechanicInfoController {

    private final MechanicInfoService mechanicInfoService;

    public MechanicInfoController(MechanicInfoService mechanicInfoService) {
        this.mechanicInfoService = mechanicInfoService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('mechanicInfo:list')")
    public void download(HttpServletResponse response, MechanicInfoQueryCriteria criteria) throws IOException {
        mechanicInfoService.download(mechanicInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询机修师")
    @ApiOperation("查询机修师")
    @PreAuthorize("@el.check('mechanicInfo:list')")
    public ResponseEntity<Object> getMechanicInfos(MechanicInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(mechanicInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增机修师")
    @ApiOperation("新增机修师")
    @PreAuthorize("@el.check('mechanicInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody MechanicInfo resources){
        return new ResponseEntity<>(mechanicInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改机修师")
    @ApiOperation("修改机修师")
    @PreAuthorize("@el.check('mechanicInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody MechanicInfo resources){
        mechanicInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除机修师")
    @ApiOperation("删除机修师")
    @PreAuthorize("@el.check('mechanicInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        mechanicInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}